<?php
    namespace Utils;
    /**
     * Cette classe sert pour toutes les fonctions qui serviront dans toute l'application
     *
     * @author mpetrini
     */
    class Outils {
        //Test si la variable a �t� envoy�
		public static function issetValue($value, $type = 'POST') {
			if($type === 'POST') {
				if(isset($_POST[$value]) && !empty($_POST[$value]))
					return true;
				else 	
					return false;	
			}
			elseif ($type === 'GET') {
				if(isset($_GET[$value]) && !empty($_GET[$value]))
					return true;
				else 	
					return false;
			
			}
		}
		
		//R�cup�re les valeurs pass�es
		public static function getValue($value, $type = 'POST') {
			$retour = '';
			if($type === 'POST') {
				if(self::issetValue($value, $type))
					$retour = $_POST[$value];
			}
			elseif($type === 'GET') {
				if(self::issetValue($value, $type))
					$retour = $_GET[$value];
			}
			return $retour;
		}
        /**
        * Fonction qui sert à tout les setters de DateTime 
        * 
        * Cette fonction retourne soit un objet DateTime soit un String
        * 
        * @param mixed $temp
        * @return mixed
        */
		public static function setterDate($temp) {
			$retour = '';
			if($temp instanceof \DateTime) {
				$retour = $temp;
            }
			else if($temp == '0000-00-00' || empty($temp)) {
				$retour = '0000-00-00';
            }
			else {
                if(\Utils\Pattern::date($temp)) {
                    $timezone = new \DateTimeZone('Europe/Paris');
                    $formats = array('Y-m-d', 'Y-m-d H:i:s', 'd/m/y', 'd/m/Y', 'H:i:s', 'H:i');
                    $i = 0;
                    while(!$retour instanceof \DateTime && isset($formats[$i])) {
                        try {
                            $retour = \DateTime::createFromFormat($formats[$i], $temp, $timezone);
                        } catch (\Exception $ex) {

                        }
                        $i++;
                    }
                }
                if(!$retour instanceof \DateTime) {
                    $retour = '0000-00-00';
                }
			}	
			return $retour;
		}
        /**
         * Fonction qui sert à tout les getters de DateTime
         * 
         * Si on passe le string 'object' alors la fonction retournera un objet DateTime, sinon cela retournera un String
         * 
         * @param mixed $temp
         * @param mixed $format
         * @return mixed
         */
		public static function getterDate($temp, $format) {
			if($temp instanceof \DateTime) {
                if($format === 'object') {
                    return $temp;
                }
                else {
                    return $temp->format($format);
                }
            }
			else {
				return $temp;
            }
		}
        /**
        * Translates a camel case string into a string with
        * underscores (e.g. firstName -> first_name)
        *
        * @param string $str String in camel case format
        * @return string $str Translated into underscore format
        */
        public static function fromCamelCase($str) {
            $str[0] = strtolower($str[0]);
            $func = create_function('$c', 'return "_" . strtolower($c[1]);');
            return preg_replace_callback('/([A-Z])/', $func, $str);
        }
 
        /**
         * Translates a string with underscores
         * into camel case (e.g. first_name -> firstName)
         *
         * @param string $str String in underscore format
         * @param bool $capitalise_first_char If true, capitalise the first char in $str
         * @return string $str translated into camel caps
         */
        public static function toCamelCase($str, $capitalise_first_char = false) {
          if($capitalise_first_char) {
            $str[0] = strtoupper($str[0]);
          }
          $func = create_function('$c', 'return strtoupper($c[1]);');
          return preg_replace_callback('/_([a-z])/', $func, $str);
        }
        /**
        * Encrypte la chaîne passé en paramètre
        * 
        * @param string $string
        * 
        * @return string
        */
        public static function encrypt($string) {
			if(!empty($string)) {
				return hash('sha512', $string);
            }
		}
        /*
        * Permet de récupérer une sous-chaîne 
        * 
        * @param string $string
        * @param int $start
        * @param int $length (optiona) (default=false)
        * @param string $encoding
        * 
        * @return string
        */
		public static function substr($string, $start, $length = false, $encoding = 'utf-8') {
			if (is_array($string)) {
				return false;
            }
			if (function_exists('mb_substr')) {
				return mb_substr($string, (int)($start), ($length === false ? strlen($string) : (int)($length)), $encoding);
            }
			return substr($string, $start, ($length === false ? strlen($string) : (int)($length)));
		}
        /**
        * Crée une chaîne aléatoire pour les mots de passe
        * 
        * @param int $length (optional) (default=8)
        * 
        * @return string
        */
		public static function randomString($length = 8) {
			$string = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$passwd = '';
			for ($i = 0; $i < $length; $i++) {
				$passwd .= self::substr($string, mt_rand(0, strlen($string) - 1), 1);
            }
			return $passwd;
		}
    }
?>